﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.ComponentModel.Composition;

namespace StoryMaker.Common
{
    [Export]
    public class StoryMakerViewModel : NotifyPropertyChangedBase
    {
        [ImportingConstructor]
        public StoryMakerViewModel([Import] ShapeViewTemplates shapeViewTemplates)
        {
            if (shapeViewTemplates == null)
            {
                throw new ArgumentNullException("shapeViewTemplates");
            }
            this.ShapeViewTemplates = shapeViewTemplates;
            Page page = new Page();
            page.Shapes.Add(ShapeViewTemplates.Shapes.First());
            Page page2 = new Page();
            page.Shapes.Add(ShapeViewTemplates.Shapes.First());
            this.Pages = new ObservableCollection<Page>();
            this.Pages.Add(page);
            this.Pages.Add(page2);
            this.CurrentPage = page;
        }

        private StoryModel storyModel;

        private ShapeViewTemplates _shapeViewTemplates;
        public ShapeViewTemplates ShapeViewTemplates
        {
            get { return _shapeViewTemplates; }
            private set
            {
                _shapeViewTemplates = value;
                NotifyPropertyChanged("ShapeViewTemplates");
            }
        }

        private bool _showAllPages;
        public bool ShowAllPages
        {
            get { return _showAllPages; }
            set
            {
                if (!EqualityComparer<bool>.Default.Equals(_showAllPages, value))
                {
                    _showAllPages = value;
                    NotifyPropertyChanged("ShowAllPages");
                    NotifyPropertyChanged("CurrentView");
                }
            }
        }

        public double MinimumPageHeight
        {
            get
            {
                return Pages.Max(x => x.Shapes.Count > 0 ? x.Shapes.Max(y => y.Height + y.Top) : 300);
            }
        }

        public double MinimumPageWidth
        {
            get
            {
                return Pages.Max(x => x.Shapes.Count > 0 ? x.Shapes.Max(y => y.Width + y.Left) : 300);
            }
        }

        private ObservableCollection<Page> _pages;
        public ObservableCollection<Page> Pages
        {
            get { return _pages; }
            set
            {
                if (_pages != value)
                {
                    _pages = value;
                    NotifyPropertyChanged("Pages");
                    NotifyPropertyChanged("IsCurrentPageLast");
                }
            }
        }

        private Page _currentPage;
        public Page CurrentPage
        {
            get { return _currentPage; }
            set
            {
                if (_currentPage != value)
                {
                    _currentPage = value;
                    NotifyPropertyChanged("CurrentPage");
                    NotifyPropertyChanged("IsCurrentPageLast");
                }
            }
        }

        public object CurrentView
        {
            get
            {
                if (ShowAllPages)
                {
                    return Pages;
                }
                return CurrentPage;
            }
        }

        private GenericCommand _nextPage;
        private GenericCommand _newPage;
        public ICommand NextPage
        {
            get
            {
                if (_nextPage == null)
                {
                    _nextPage = new GenericCommand(() => CurrentPage = Pages[Pages.IndexOf(CurrentPage) + 1]);
                }
                if (_newPage == null)
                {
                    _newPage = new GenericCommand(newPage);
                }
                return IsCurrentPageLast ? _newPage : _nextPage;
            }
        }
        public bool IsCurrentPageLast
        {
            get { return CurrentPage != Pages.Last(); }
        }

        private void newPage()
        {
            Pages.Add(new Page());
            _nextPage.Execute(null);
        }

        private GenericCommand _previousPage;
        public ICommand PreviousPage
        {
            get
            {
                if (_previousPage == null)
                {
                    _previousPage = new GenericCommand(() => CurrentPage = Pages[Pages.IndexOf(CurrentPage) - 1], x => CurrentPage != Pages.First());
                }
                return _previousPage;
            }
        }
    }
}
